package com.situ.store.dao;

import java.util.List;

import com.situ.store.entity.Goods;
import com.situ.store.util.DBCP;

public class GoodsDao {

	// 添加货物
	public void add(Goods goods) {
		String sql = "INSERT INTO t_goods(name,factory,model,specs) VALUES(?,?,?,?)";
		DBCP.excuteDML(sql, goods.getName(), goods.getFactory(), goods.getModel(), goods.getSpecs());
	}

	// 删除货物
	public void del(Integer id) {
		String sql = "UPDATE t_goods SET sta = 0 WHERE id = ?";
		DBCP.excuteDML(sql, id);
	}

	// 修改货物
	public void edit(Goods goods) {
		String sql = "UPDATE t_goods SET name = ?,factory = ?,model = ?, specs = ? WHERE id = ?";
		DBCP.excuteDML(sql, goods.getName(), goods.getFactory(), goods.getModel(), goods.getSpecs(), goods.getId());
	}

	// 查询所有货物
	public List<Goods> selectAll(Integer pageSize, Integer pageNum, String keyword1, String keyword2) {
		String sql = "SELECT * FROM t_goods WHERE INSTR(name, '" + keyword1 + "') > 0 AND INSTR(factory,'" + keyword2
				+ "') > 0 AND sta = 1 LIMIT ?, ? ";
		return DBCP.excuteDQL(sql, Goods.class, pageSize, pageNum);
	}

	// 根据ID查询货物
	public Goods selectById(Integer id) {
		String sql = "SELECT * FROM t_goods WHERE sta = 1 AND id = ?";
		List<Goods> l = DBCP.excuteDQL(sql, Goods.class, id);
		return l.size() > 0 ? l.get(0) : null;
	}

	// 查数量，用于分页查询
	public Long count(String keyword1, String keyword2) {
		String sql = "SELECT COUNT(1) FROM t_goods WHERE INSTR(`name`, '" + keyword1 + "') > 0 AND INSTR(factory, '"
				+ keyword2 + "') > 0 AND sta = 1";
		return DBCP.executeCount(sql);
	}

	public List<Goods> selectName() {
		String sql = "SELECT id, `name` FROM t_goods WHERE sta = 1";
		return DBCP.excuteDQL(sql, Goods.class);
	}
	
	public List<Goods> selectFactory() {
		String sql = "SELECT DISTINCT factory FROM t_goods";
		return DBCP.excuteDQL(sql, Goods.class);
	}
	

}
